package cn.wtg.kotlinlib.baseViewModel

import android.content.Context
import cn.wtg.kotlinlib.baseUi.BaseActivity
import cn.wtg.kotlinlib.baseUi.BaseFragment
import org.greenrobot.eventbus.EventBus

/**
 * author: wtg
 * date:2020/3/13 0013
 * desc: ViewModel的基类 fragment(适用单fragment)
 */
abstract class BaseFragmentLifecycleVM : BaseLifecycleVM {
    protected var mFragment: BaseFragment? = null
    protected var mActivity: BaseActivity? = null
    protected var mContext: Context? = null

    constructor(fragment: BaseFragment?) {
        mFragment = fragment
        mContext = this.mFragment!!.requireContext()
        this.initData()
    }

    constructor(fragment: BaseFragment?, mActivity: BaseActivity?) {
        mFragment = fragment
        this.mActivity = mActivity
        mContext = mFragment?.context
        this.initData()
    }

    /**
     * 初始化
     */
    open fun initData(){}

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }
}